package com.htjc.integral.conversion.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.htjc.integral.conversion.enums.Constant;
import com.htjc.integral.conversion.mapper.EcBpExchangeMapper;
import com.htjc.integral.conversion.service.IEcCorporationBoundService;
import com.htjc.integral.conversion.service.IEcIntegralDetailService;
import com.htjc.integral.conversion.service.IEcbpExchangeService;
import com.htjc.integral.conversion.service.IGdtService;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "conversionLogger")
@Service
public class EcBpExchangeServiceImpl implements IEcbpExchangeService{
	@Autowired
	EcBpExchangeMapper ecBpExchangeMapper;
	
	@Autowired
	IGdtService gdtServiceImpl;
	
	@Autowired
	IEcCorporationBoundService ecCorporationBoundServiceImpl;
	
	@Autowired
	IEcIntegralDetailService ecIntegralDetailServiceImpl;
	
	@Override
	public List<Map<String, Object>> queryCorExchange(Map<String, Object> map) {
		return this.ecBpExchangeMapper.queryCorExchange(map);
	}

	//111
	@Override
	public Map<String, Object> selectProvince(String applicationNo,String corporationId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("applicationNo", applicationNo);
		map.put("corporationId", corporationId);
		return this.ecBpExchangeMapper.selectProvince(map);
	}

	//111
	@Override
	public int updateBpProvince(int corporationOvalue,int corporationDvalue,
			String corporationId,String applicationNo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("corporationOvalue", corporationOvalue);
		map.put("corporationDvalue", corporationDvalue);
		map.put("corporationId", corporationId);
		map.put("applicationNo", applicationNo);
		return this.ecBpExchangeMapper.updateBpProvince(map);
	}

	//111
	/**
	 * 平台积分转换业务处理
	 * @param orgNo
	 * @param corporationId
	 * @param calendar
	 * @param score
	 * @param diffB
	 * @param scaleMallB
	 * @param scalePaltformB
	 * @param partnerId
	 * @param boundCorId
	 * @param consNo
	 * @return
	 */
	@Override
	public void saveCoverScorePro(String score,String boundCorId,String uuid
			,String partnerId,int reB,Calendar calendar,String orgNo,
			String corporationId,String consNo,String scores,String extvar) throws Exception{
			//更新绑定表
			this.ecCorporationBoundServiceImpl.updateValueByCode(Integer.valueOf(scores),Integer.valueOf(reB), "", boundCorId, "");
			/*//积分流水 boolean insertBpMemberflow= 
			this.saveBpMemberflow(uuid, partnerId, 
					reB, Integer.valueOf(score).intValue(), calendar.getTime(),"合作平台积分转换新增(用电编号："+consNo+")", orgNo, 
					calendar.get(Calendar.YEAR), "1", boundCorId);
			//插入积分流水明细 boolean insertBpMemberflowDetail = 
			this.saveBpMemberflowDetail(UUIDGenerator.getInstance().getReplaceUUID(), uuid, boundCorId,
					reB, calendar.get(Calendar.YEAR), calendar.get(Calendar.YEAR), "合作平台积分转换新增(用电编号："+consNo+")");
			//更新年度总积分 boolean updateBpMember = 
			this.updateAnnualIntegralValueForConver(partnerId, calendar.get(Calendar.YEAR), reB);*/
			
			//新的积分记录方法
			Map<String, Object> userMap=new HashMap<String, Object>();
            userMap.put("userId", partnerId);
            userMap.put("totalNum", reB);
            userMap.put("originId", uuid);
            userMap.put("scaleTrans", Integer.valueOf(score));
            userMap.put("bpTransition", "1");//网省用的积分
            userMap.put("remark", "合作平台积分转换新增(用电编号："+consNo+")");
            userMap.put("consNo", consNo);
            userMap.put("boundCorId", boundCorId);	//绑定ID
            userMap.put("orgNo", orgNo);	//网省编号
            userMap.put("extvar", extvar);
            Map<String, String> sourceMap=new HashMap<String, String>();
            sourceMap.put("bizCodeIdCreate", "DLJF_JF");
            
            sourceMap.put("channelIdCreate", Constant.getSourceName(orgNo));
            //sourceMap.put("channelIdCreate", "ESGCC");
            ecIntegralDetailServiceImpl.changeIntegralAndGlobalLock(true,userMap,sourceMap);
            
			//更新积分池 int updateBpProvinceCount = 
			//this.updateBpProvince(reB, reB, corporationId, orgNo);
	}
	
	@Override
	public int saveBpMemberflowRecord(String id, String partnerId,
			String bpValue, String scaleTrans, Date createdTime, String remark,
			String bpYear, String boundId, String applicationNo,
			String successOrFail) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("partnerId", partnerId);
		params.put("bpValue", bpValue);
		params.put("scaleTrans", scaleTrans);
		params.put("createdTime", createdTime);
		params.put("remark", remark);
		params.put("bpYear", bpYear);
		params.put("boundId", boundId);
		params.put("applicationNo", applicationNo);
		params.put("successOrFail", successOrFail);
		
		return this.ecBpExchangeMapper.insertBpMemberflowRecord(params);
	}

	@Override
	public int updateBpProvinceFH(int corporationOvalue, int corporationDvalue,
			String corporationId, String applicationNo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("corporationOvalue", corporationOvalue);
		map.put("corporationDvalue", corporationDvalue);
		map.put("corporationId", corporationId);
		map.put("applicationNo", applicationNo);
		return this.ecBpExchangeMapper.updateBpProvinceFH(map);
	}
}
